package com.wholesmart.common.security.authentication;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wholesmart.common.bean.Message;

/**
 * Spring Security退出成功处理器
 * <p>
 * 当对用户退出时交由该类处理。
 * <p>
 * 主要用于自定义返回前端的Json消息，当然你也可以在这里根据需求做页面跳转。
 * <p>
 * 需要手动配置到WebSecurityConfigurerAdapter的实现内中。
 * 
 * @author dyw
 * @date 2019年11月4日
 */
public class WholesmartLogoutSuccessHandler implements LogoutSuccessHandler {
	private Logger logger = LoggerFactory.getLogger(getClass());
	private static final ObjectMapper objectMapper = new ObjectMapper();

	@Override
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {
		logger.info("退出成功");
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().write(objectMapper.writeValueAsString(Message.success("退出成功")));
	}
}
